package cz.vutbr.feec.utko.ef.examples.sum;

import cz.vutbr.feec.utko.ef.core.Config;
import cz.vutbr.feec.utko.ef.core.GProgram;
import cz.vutbr.feec.utko.ef.evolution.Chromozome;
import cz.vutbr.feec.utko.ef.evolution.DefaultEvolutionSpecifierTree;
import cz.vutbr.feec.utko.ef.evolution.DisplayType;
import cz.vutbr.feec.utko.ef.evolution.IEvolutionSpecifier;
import cz.vutbr.feec.utko.ef.gp.tree.TreeChromozome;
import cz.vutbr.feec.utko.ef.grammar.Grammar;
import cz.vutbr.feec.utko.ef.grammar.Rule;
import cz.vutbr.feec.utko.ef.individuals.ActionTree;
import cz.vutbr.feec.utko.ef.individuals.MutationIsNotPossibleException;
import cz.vutbr.feec.utko.ef.terminals.IntegerTerminal;

/**
 * The Class MaxSumExample.
 */
public class MaxSumExample {
	
	/**
	 * The main method.
	 * 
	 * @param args
	 *            the arguments
	 * 
	 * @throws MutationIsNotPossibleException
	 *             the mutation is not possible exception
	 */
	public static void main(String[] args) throws MutationIsNotPossibleException {

		ActionTree number = new IntegerTerminal((short) 0, (short) 10);
//		Action inputParam = new IntegerTerminal((short) 0, (short) 10);
		
		Grammar g = new Grammar();
//		g.registerInputParameter(inputParam);
		g.addRule("ROOT", new Rule("E"));

		System.out.println(g.getNumberOfProgramInputParameters());
//		IntegerTerminal t = (IntegerTerminal)g.getProgramInputParameters(0);
//		t.setValue(10);
		
		// add the rules to the grammar
		g.addRule("E", 
				new Rule(new PlusAction(), "E", "E"), 
//				new Rule(new MinusAction(), "E", "E"), 
//				new Rule(inputParam),
				new Rule(number)
		);

		g.toString();
		
		FitnessEvaluatorExample fitness = new FitnessEvaluatorExample();
		Config cfg = new Config("config_default.ini");
		IEvolutionSpecifier s = new DefaultEvolutionSpecifierTree(cfg, fitness, g);
		GProgram gp = new GProgram(cfg, s);

		gp.evolve();

		Chromozome ch = gp.getPopulation().getBestIndividual();
		ch.visualize("out.svg", DisplayType.WITH_IMAGES);

		System.out.println("Fitness: " + ch.getCachedFitness());
		System.out.println();
		System.out.println(((TreeChromozome)ch).generateCode());
	}
}
